﻿Imports System.Data.SqlClient

Public Class common


    Public Function CheckConnection(ByVal cstring)

        Dim ConnectionString As String = cstring
        Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
        Try
            DBConnection.Open()
        Catch ex As Exception

            ' Close the database connection if it is still open.
            If DBConnection.State = ConnectionState.Open Then

                CheckConnection = "Success"
                Exit Try
                Exit Function

            Else
                CheckConnection = "Unable to connect to the database. " & ex.Message

                Exit Try

            End If
        End Try

    End Function

    Function RunQuery(ByVal QueryString As String) As DataSet

        ' Declare the connection string. This function uses Microsoft SQL Server and connects to the
        ' test_case_db  database.
        Dim ConnectionString As String = ConnectDatabase.GetRegistryValue("stringcon")
        Dim DBConnection As SqlConnection = New SqlConnection(ConnectionString)
        Dim DBAdapter As SqlDataAdapter
        Dim ResultsDataSet As DataSet = New DataSet

        Try

            ' Run the query and create a DataSet.
            DBAdapter = New SqlDataAdapter(QueryString, DBConnection)
            DBAdapter.Fill(ResultsDataSet)

            ' Close the database connection.
            DBConnection.Close()

        Catch ex As Exception

            ' Close the database connection if it is still open.
            If DBConnection.State = ConnectionState.Open Then

                DBConnection.Close()

            Else

                MsgBox("Unable to connect to the database." & ex.Message)
                CheckConnection(ConnectionString)

                Exit Try

            End If
        End Try

        Return ResultsDataSet

    End Function



    'Public Sub Load_tree()

    '    Dim PrSet As DataSet = RunQuery("SELECT * FROM TreeViewData")

    '    Dashboard.TreeView1.Nodes.Clear()

    '    For Each dr As DataRow In PrSet.Tables(0).Rows
    '        Dim tnParent As New TreeNode()

    '        If DirectCast(dr("Parent_Id"), Integer) = 0 Then
    '            tnParent.Text = dr("Node_Name").ToString()
    '            tnParent.Expand()
    '            tv.Nodes.Add(tnParent)

    '        ElseIf DirectCast(dr("Parent_Id"), Integer) = 1 Then

    '            Dim child As New TreeNode()
    '            child.Text = dr("Node_Name").ToString()
    '            child.Expand()
    '            tv.TopNode.Nodes.Add(child)
    '            child.Checked = True
    '            FillChild(dr("Id").ToString(), child)

    '        End If

    '    Next

    'End Sub



    'Public Function FillChild(ByVal IID As String, ByVal parentnode As TreeNode)

    '    Dim ds As DataSet = RunQuery("SELECT * FROM TreeViewData WHERE Parent_Id =" & IID)

    '    For Each dr As DataRow In ds.Tables(0).Rows

    '        Dim childs As New TreeNode()
    '        childs.Text = dr("Node_Name").ToString()
    '        childs.Expand()
    '        parentnode.Nodes.Add(childs)
    '        FillChild(dr("Id").ToString(), childs)

    '    Next

    '    Return 0

    'End Function


    Public Sub Load_trees()
        Explorer1.TreeView.Nodes.Clear()
        Dim PrSet As DataSet = RunQuery("SELECT * FROM tree_views")
        Dashboard_old.TreeView1.Nodes.Clear()

        For Each dr As DataRow In PrSet.Tables(0).Rows
            Dim tnParent As New TreeNode()
            ' MsgBox(dr("Node_Title").ToString())
            If (dr("Parent_Node").ToString()) = "root" Then
                tnParent.Text = dr("Node_Title").ToString()
                tnParent.ToolTipText = dr("Node_Description").ToString()
                tnParent.Expand()
                tnParent.ContextMenuStrip = Explorer1.NodeContextMenu
                tnParent.ImageIndex = 1
                tnParent.SelectedImageIndex = 0
                tnParent.Tag = dr("Node_Title").ToString()
                Explorer1.TreeView.Nodes.Add(tnParent)

            End If
            If dr("Node_Level").ToString() = 1 Then
                Dim child As New TreeNode()
                child.Text = dr("Node_Title").ToString()
                child.ToolTipText = dr("Node_Description").ToString()
                child.ContextMenuStrip = Explorer1.NodeContextMenu
                child.ImageIndex = 3
                child.SelectedImageIndex = 2
                child.Tag = dr("Node_Title").ToString()
                child.Expand()
                Explorer1.TreeView.TopNode.Nodes.Add(child)
                child.Checked = True
                FillChilds(dr("Id").ToString(), child)

            End If

        Next

    End Sub



    Public Function FillChilds(ByVal IID As String, ByVal parentnode As TreeNode)

        Dim ds As DataSet = RunQuery("SELECT * FROM tree_views WHERE Node_Level =" & IID)

        For Each dr As DataRow In ds.Tables(0).Rows

            Dim childs As New TreeNode()
            childs.Text = dr("Node_Title").ToString()
            childs.ToolTipText = dr("Node_Description").ToString()
            childs.ContextMenuStrip = Explorer1.NodeContextMenu
            childs.ImageIndex = 3
            childs.SelectedImageIndex = 2
            childs.Tag = dr("Node_Title").ToString()
            childs.Expand()
            parentnode.Nodes.Add(childs)

            FillChilds(dr("Id").ToString(), childs)

        Next

        Return 0

    End Function


  
End Class